package com.restaurant.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.restaurant.service.UserDao;
import com.restaurant.bean.User;
import com.restaurant.service.UserService;
import com.restaurant.service.impl.UserServiceImpl;
import com.restaurant.util.LoginConfig;
import com.restaurant.util.Result;
import com.restaurant.util.StringUtil;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class LoginFrame extends JFrame {
	private JPanel contentPane;
	private JPasswordField UserPassWordTxt;
	private JTextField UserNameTxt;
	private UserService service = new UserServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setTitle("登陆");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("幽灵点餐");
		lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 20));
		lblNewLabel.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/ghost.png")));
		
		JLabel lblNewLabel_1 = new JLabel("手机号");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/姓名 用户名 昵称.png")));
		lblNewLabel_1.setToolTipText("");
		
		JLabel lblNewLabel_2 = new JLabel("密码");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_2.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/密码.png")));
		
		UserPassWordTxt = new JPasswordField();
		
		UserNameTxt = new JTextField();
		UserNameTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("登陆");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginActionPerformed(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/登陆.png")));
		
		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegActionPerformed(e);
			}

		});
		btnNewButton_1.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/注册.png")));
		
		JLabel lblNewLabel_3 = new JLabel("权限");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 12));
		lblNewLabel_3.setIcon(new ImageIcon(LoginFrame.class.getResource("/images/权限.png")));
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 12));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"用户", "管理员"}));
		
		JButton btnNewButton_2 = new JButton("重置");
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 12));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(131)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(77)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
								.addComponent(lblNewLabel_2))
							.addGap(5)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(UserPassWordTxt, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
								.addComponent(UserNameTxt, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(119)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addComponent(btnNewButton_1)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addGap(34)
									.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
					.addGap(76))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(49)
					.addComponent(btnNewButton)
					.addPreferredGap(ComponentPlacement.RELATED, 187, Short.MAX_VALUE)
					.addComponent(btnNewButton_2)
					.addGap(38))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(24)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
					.addGap(15)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(UserNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(25)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel_2)
						.addComponent(UserPassWordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap())
		);
		gl_contentPane.linkSize(SwingConstants.VERTICAL, new Component[] {btnNewButton, btnNewButton_1, btnNewButton_2});
		gl_contentPane.linkSize(SwingConstants.VERTICAL, new Component[] {UserPassWordTxt, UserNameTxt});
		gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnNewButton, btnNewButton_1, btnNewButton_2});
		gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {UserPassWordTxt, UserNameTxt});
		contentPane.setLayout(gl_contentPane);
		//居中显示 
		this.setLocationRelativeTo(null);
	}
	protected void RegActionPerformed(ActionEvent e) {
		//销毁当前窗体
		this.dispose();
		//让注册的窗体可见
		new RegFrame().setVisible(true);;
	}

	/**
	 * 对登陆事件的处理
	 * @param e
	 */
	protected void LoginActionPerformed(ActionEvent e) {
		String phoneNumber = this.UserNameTxt.getText();
		String password = new String(this.UserPassWordTxt.getPassword());
        Result result = service.isPhoneOrEmail(phoneNumber, password);
		if(result.getStatus() == 200) {//登陆成功
			JOptionPane.showMessageDialog(null, result.getMsg());
			LoginConfig.save((User) result.getData());
			this.dispose();
			new MainFrame().setVisible(true);
		}else {//登陆失败
			JOptionPane.showMessageDialog(null, result.getMsg());
		}
		System.out.println(LoginConfig.reader().get(0));
		System.out.println(LoginConfig.reader().get(1));
	}
	
	public void reset() {
		this.UserNameTxt.setText("");
		this.UserPassWordTxt.setText("");
	}
}
